module Tuscan
  module Iec60584
    module TypeJ
      T90_RANGE = -210.0..1200.0
      EMF_RANGE = -8.096..69.554

      extend self

      def emfr_unbound t90
        if t90 < 760.0
          Polynomial.new(
             0.000000000000e+00,  0.503811878150e-01,  0.304758369300e-04, -0.856810657200e-07,
             0.132281952950e-09, -0.170529583370e-12,  0.209480906970e-15, -0.125383953360e-18,
             0.156317256970e-22
          ).solve_for t90
        else
          Polynomial.new(
             0.296456256810e+03, -0.149761277860e+01,  0.317871039240e-02, -0.318476867010e-05,
             0.157208190040e-08, -0.306913690560e-12
          ).solve_for t90
        end
      end

      def t90r_guess emf
        if emf < 0.0
          Polynomial.new(
             0.0000000e+00,  1.9528268e+01, -1.2286185e+00, -1.0752178e+00, -5.9086933e-01,
            -1.7256713e-01, -2.8131513e-02, -2.3963370e-03, -8.3823321e-05
          ).solve_for emf
        elsif emf < 42.919
          Polynomial.new(
             0.000000e+00,   1.978425e+01,  -2.001204e-01,   1.036969e-02,  -2.549687e-04,
             3.585153e-06,  -5.344285e-08,   5.099890e-10
          ).solve_for emf
        else
          Polynomial.new(
             -3.11358187e+03, 3.00543684e+02, -9.94773230e+00, 1.70276630e-01, -1.43033468e-03,
              4.73886084e-06
          ).solve_for emf
        end
      end
    end
  end
end

